package com.ebelter.myehc.models.http.request;

import android.text.TextUtils;

import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.RateBean;

import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.SleepArrayInfo;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.WalkBean;
import com.ebelter.btcomlib.models.https.MyStringCallback;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.myehc.models.http.constant.HttpConstant;
import com.ebelter.myehc.models.http.response.EhcLoginResponse;
import com.ebelter.myehc.models.http.response.GetBC;
import com.ebelter.myehc.models.http.response.GetBG;
import com.ebelter.myehc.models.http.response.GetBO;
import com.ebelter.myehc.models.http.response.GetBP;
import com.ebelter.myehc.models.http.response.GetBra;
import com.ebelter.myehc.models.http.response.GetCholesterol;

import com.ebelter.myehc.models.http.response.GetPersonInfoFromBT;
import com.ebelter.myehc.models.http.response.GetTemperature;
import com.ebelter.myehc.models.http.response.GetUA;
import com.ebelter.myehc.models.http.response.RegisterBT;
import com.ebelter.myehc.models.http.response.device_resports.DeviceResportParseJs;
import com.ebelter.myehc.models.http.response.ehc.EhcLoginBt;
import com.ebelter.myehc.models.http.response.others.GetNewestBraHeartTime_R;
import com.ebelter.myehc.models.update.UpdateVersionBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EhcNetUtils{

    private static final String TAG = "EhcNetUtils";

    private static EhcNetUtils instance;
    private EhcJsonUtil mEhcJsonUtil;
    private EhcNetUtils() {
        mEhcJsonUtil=new EhcJsonUtil();
    }

    public static EhcNetUtils getInstance() {
        if (instance==null){
            synchronized (EhcNetUtils.class){
                if (instance==null) instance=new EhcNetUtils();
            }
        }
        return instance;
    }

    /**
     * postJson请求获取数据
     */
    private void postJson(Object tag, String url, String requestJson, StringCallback callback){
        OkGo.post(url).tag(tag).upJson(requestJson).execute(callback);
    }

    private void postJsonEhc(Object tag, String requestJson, StringCallback callback){
        OkGo.post(HttpConstant.requestUrl).tag(tag).upJson(requestJson).execute(callback);
    }

    public void cancelTag(Object tag){
        OkGo.getInstance().cancelTag(tag);
    }


    //获取手环成分列表
    public void getBraPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetBra> httpResponse){
        String json = mEhcJsonUtil.getBraPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetBra.class));
    }



    //上传体温
    public void upLoadTemperature(Object tag,String authKey,float temperature, String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getUpLoadTemperatureJS(authKey,temperature,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取人体成分列表
    public void getTemperaturePage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetTemperature> httpResponse){
        String json = mEhcJsonUtil.getTemperaturePageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetTemperature.class));
    }

    //上传血氧
    public void upLoadBloodOxygen(Object tag,String authKey,float bloodoxygen, int pulse,String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getUpLoadBloodOxygenJS(authKey,bloodoxygen,pulse,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取血氧列表
    public void getBloodOxygenPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetBO> httpResponse){
        String json = mEhcJsonUtil.getBloodOxygenPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetBO.class));
    }

    //上传血氧 type = 1空腹 2餐前 3餐后
    public void upLoadBloodSugar(Object tag,String authKey,float bloodsugar, int type,String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadBloodSugarJS(authKey,bloodsugar,type,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }
    //获取血糖列表
    public void getBloodSugarPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetBG> httpResponse){
        String json = mEhcJsonUtil.getBloodSugarPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetBG.class));
    }

    //上传血压
    public void upLoadBloodPressure(Object tag,String authKey,int systolic, int diastolic,int pulse,String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadBloodPressureJS(authKey,systolic,diastolic,pulse,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取血压列表
    public void getBloodPressurePage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetBP> httpResponse){
        String json = mEhcJsonUtil.getBloodPressurePageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetBP.class));
    }

    //上传尿酸
    public void upLoadUa(Object tag,String authKey,int ua,String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadUaJS(authKey,ua,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取尿酸
    public void getUAPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetUA> httpResponse){
        String json = mEhcJsonUtil.getUAPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetUA.class));
    }


    //上传胆固醇
    public void upLoadCholesterol(Object tag,String authKey,float cholesterol,String time, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadCholesterolJS(authKey,cholesterol,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //上传心率
    public void upLoadXinLv(Object tag, String authKey, List<RateBean> lists, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadXinLvJS(authKey,lists);
        LogUtils.i(TAG,"上传心率 = " + json);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //上传睡眠
    public void upLoadShuiMian(Object tag, String authKey, List<SleepArrayInfo> lists, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadSleepJS(authKey,lists);
        LogUtils.i(TAG,"上传睡眠 = " + json);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //上传步数、距离、卡路里数据接口
    public void upLoadStepsDistanceCariliLv(Object tag, String authKey, List<WalkBean> lists, final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadStepsDistanceCariliJS(authKey,lists);
        LogUtils.i(TAG,"上传步数、距离、卡路里数据接口 = " + json);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取人体成分列表
    public void getCholesterolPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetCholesterol> httpResponse){
        String json = mEhcJsonUtil.getCholesterolPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetCholesterol.class));
    }


    //上传人体成分
    public void upLoadBodyComposition(Object tag,String authKey,float weight, float adiposerate,float moisture,
                                      int basalMetabolism,float visceralfat, float muscle,float bone,
                                      float bmi,float protein,String time,final HttpResponse<BaseResponse> httpResponse){
        String json = mEhcJsonUtil.getLoadBodyCompositionJS(authKey,weight,adiposerate,moisture,basalMetabolism,visceralfat,muscle,bone,bmi,protein,time);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //获取人体成分列表
    public void getBodyCompositionPage(Object tag,String authKey,int pageNo,int pageSize, final HttpResponse<GetBC> httpResponse){
        String json = mEhcJsonUtil.getBodyCompositionPageJS(authKey,pageNo,pageSize);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetBC.class));
    }


    //获取设备报表数据
    //measureType  1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份 8:锻炼 9:心率 10:睡眠
    //dateType  1:年 2:月 3周: 4:天
    //param  参数值 年：2018
    // 月2018-01
    // 周 2018-01-01（转开始日期就行，后台自动T+7）
    // 天 2018-01-02
    public void getDeviceReportData(Object tag, final String authKey, final int measureType, final int dateType, final String param, final HttpResponse<BaseResponse> httpResponse2){

        String json = mEhcJsonUtil.getDeviceReportDataJS(authKey,measureType,dateType,param);//生成请求的json
        postJsonEhc(tag,json,new MyStringCallback<>(new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (isRequestSuccess&&baseResponse.resultCode==1){//如果请求成功根据返回的s去生成真正的baseResponse对象
                    baseResponse  =   DeviceResportParseJs.parseJs(baseResponse,measureType,s);
                }
                httpResponse2.result(isRequestSuccess,requestDes,baseResponse,s);
            }
        }, BaseResponse.class));
    }






    //第三方登录
    public void ehcLogin(Object tag,String username,String psw,final HttpResponse<EhcLoginResponse> httpResponse){
        String json = mEhcJsonUtil.getLoginJs(username,psw);
        postJson(tag,"http://159.89.195.45/ehc/api/oauth/token",json,new MyStringCallback<>(httpResponse,EhcLoginResponse.class));
    }


//    //第三方获取用户信息
//    public void getPersonInfo(Object tag,String accessToken,final HttpResponse<GetPersonInfo> httpResponse){
//        OkGo.get("http://159.89.195.45/ehc/api/my/profile").tag(tag).headers("Authorization",accessToken).execute(new MyStringCallback<>(httpResponse,GetPersonInfo.class));
//    }



    //根据id查询用户存在 如果存在返回用户信息
    public void queryUserIsExitById(Object tag,String userId,final HttpResponse<GetPersonInfoFromBT> httpResponse){
        String json = mEhcJsonUtil.getQueryUserIsExitByIdJS(userId);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetPersonInfoFromBT.class));
    }

    //倍泰注册
    public void registerUserBT(Object tag,String name,String userName,String sex,String birthday,String height,
                             String weight,String dtype,String softver,String userId,String sn,String pwd,final HttpResponse<RegisterBT> httpResponse){
        String json = mEhcJsonUtil.getRegisterJS(name,userName,sex,birthday,height,weight,dtype,softver,userId,sn,pwd);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,RegisterBT.class));
    }



    //sex 是0男1女的标准
    public void updatePersonSex(Object tag,String authKey,int sex,final HttpResponse<BaseResponse> httpResponse){
        Map<String, Serializable> paramMap = new HashMap<>();
        String sex2;
        if (sex==0){//男 本地0男1女
            sex2="1";//服务器上1代表男
        }else {
            sex2="2";//服务器上2代表女
        }
        paramMap.put("sex",sex2);
        String json = mEhcJsonUtil.getUpdatePersonInfo(paramMap,authKey);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //修改生日 传进来的是2018-10-10的格式
    public void updatePersonBirthday(Object tag,String authKey,String birthday,final HttpResponse<BaseResponse> httpResponse){
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("birthday",birthday);
        String json = mEhcJsonUtil.getUpdatePersonInfo(paramMap,authKey);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //修改身高
    public void updatePersonHeight(Object tag,String authKey,float height,final HttpResponse<BaseResponse> httpResponse){
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("height",height);
        String json = mEhcJsonUtil.getUpdatePersonInfo(paramMap,authKey);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }

    //修改体重
    public void updatePersonWeight(Object tag,String authKey,float weight,final HttpResponse<BaseResponse> httpResponse){
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("weight",weight);
        String json =mEhcJsonUtil.getUpdatePersonInfo(paramMap,authKey);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,BaseResponse.class));
    }


//    {
//        "bizId": "08",
//            "funcId": "121",
//            "version": "01",
//            "authKey": "LTEjNiM5IzkyIzE1MTY0MzU1OTc0NjkjNDQwMzA2MDAwMDAwMDAwMDMjMzQ=",
//            "data":{}
//    }
    public void getNewestBraHeartTime(Object tag,String authKey,final HttpResponse<GetNewestBraHeartTime_R> httpResponse){
        String json =mEhcJsonUtil.getNewestBraHeartTime(authKey);
        postJsonEhc(tag,json,new MyStringCallback<>(httpResponse,GetNewestBraHeartTime_R.class));
    }


    //ehc 第三方给的登录
    //phone_region - 852 / 86
    //phone_no 手机号码
    //device_type  - Android /IOS
    //device_token
    public void ehcLogin2(Object tag,String phone_no,final String password,final HttpResponse<EhcLoginBt> httpResponse){
        if (TextUtils.isEmpty(phone_no)){
            return;
        }
        HttpParams params = new HttpParams();
        int loacal = 852;
        if (phone_no.length()==11){
            loacal=86;
        }

        params.put("phone_region",loacal);
        params.put("phone_no",phone_no);
        params.put("password",password);
        params.put("device_type","Android");
        params.put("device_token","");

        OkGo.post("http://ehclogin.apptechclient.com/api/login").tag(tag)
                .params(params).execute(new MyStringCallback<>(httpResponse,EhcLoginBt.class));
    }





    public void getUpdateVersionJson(Object tag, final HttpResponse<UpdateVersionBean> httpResponse){
        OkGo.post("http://o.ebelter.com/ehc_apk/version.json").tag(tag).execute(new MyStringCallback<>(httpResponse,UpdateVersionBean.class));
    }


}
